package lu.med.praxiswahl.wahlumed.client.accountmanagement;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;


/**
 * 
 * @author Lars Holger Roth
 * 
 */
@SuppressWarnings("serial")
public class UserAccountDialog extends JDialog implements ActionListener
{
    private JTextField a;
    private JPasswordField b;
    private JCheckBox c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r;
    private Main dataManager;
    private JCheckBox cashBook;

    public UserAccountDialog(Main dataManager)
    {
	super(dataManager, "Benutzerkonto anlegen", true);
	this.setLayout(new BorderLayout());
	this.dataManager = dataManager;

	JPanel buttonPanel, rechtePanel;
	buttonPanel = new JPanel();
	JButton aa, ab, ac, ad;
	aa = new JButton("Konto anlegen");
	ab = new JButton("Abbrechen");
	ac = new JButton("L" + UniCode.getoe() + "schen");
	ad = new JButton("Passwort " + UniCode.getae() + "ndern");
	aa.addActionListener(this);
	ab.addActionListener(this);
	ac.addActionListener(this);
	ad.addActionListener(this);
	buttonPanel.add(aa);
	buttonPanel.add(ac);
	buttonPanel.add(ad);
	buttonPanel.add(ab);
	this.add(buttonPanel, BorderLayout.SOUTH);

	rechtePanel = new JPanel(new GridLayout(0, 2));
	rechtePanel.add(new JLabel("Benutzername:"));
	a = new JTextField();
	rechtePanel.add(a);
	rechtePanel.add(new JLabel("Passwort:"));
	b = new JPasswordField();
	rechtePanel.add(b);
	rechtePanel.add(new JLabel("Administrator:"));
	c = new JCheckBox();
	rechtePanel.add(c);
	rechtePanel.add(new JLabel("Analysen Intern:"));
	d = new JCheckBox();
	rechtePanel.add(d);
	rechtePanel.add(new JLabel("Arztbriefe:"));
	e = new JCheckBox();
	rechtePanel.add(e);
	rechtePanel.add(new JLabel("Arzt Datenbank:"));
	f = new JCheckBox();
	rechtePanel.add(f);
	rechtePanel.add(new JLabel("Grav Test:"));
	g = new JCheckBox();
	rechtePanel.add(g);
	rechtePanel.add(new JLabel("Kalender:"));
	h = new JCheckBox();
	rechtePanel.add(h);
	rechtePanel.add(new JLabel("Laborbuch:"));
	i = new JCheckBox();
	rechtePanel.add(i);
	rechtePanel.add(new JLabel("Lieferanten:"));
	j = new JCheckBox();
	rechtePanel.add(j);
	rechtePanel.add(new JLabel("Mahnungen:"));
	k = new JCheckBox();
	rechtePanel.add(k);
	rechtePanel.add(new JLabel("OP Rechnungen"));
	l = new JCheckBox();
	rechtePanel.add(l);
	rechtePanel.add(new JLabel("Ordonancen:"));
	m = new JCheckBox();
	rechtePanel.add(m);
	rechtePanel.add(new JLabel("Patientenmaske:"));
	n = new JCheckBox();
	rechtePanel.add(n);
	rechtePanel.add(new JLabel("Rechnungen:"));
	o = new JCheckBox();
	rechtePanel.add(o);
	rechtePanel.add(new JLabel("Tarife:"));
	p = new JCheckBox();
	rechtePanel.add(p);
	rechtePanel.add(new JLabel("Zytologiezettel:"));
	q = new JCheckBox();
	rechtePanel.add(q);
	rechtePanel.add(new JLabel("Zytologiebuch:"));
	r = new JCheckBox();
	rechtePanel.add(r);
	
	cashBook = new JCheckBox();
	rechtePanel.add(cashBook);
	
	this.add(rechtePanel, BorderLayout.CENTER);

	this.pack();
	this.setVisible(true);

    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
	if (e.getActionCommand().equals("Konto anlegen"))
	{
	    UserAccount acc = new UserAccount();
	    acc.setAdmin(c.isSelected());
	    acc.setAnalysenIntern(d.isSelected());
	    acc.setArztbriefe(this.e.isSelected());
	    acc.setArztdatabase(f.isSelected());
	    acc.setGravtest(g.isSelected());
	    acc.setKalender(h.isSelected());
	    acc.setLaborbuch(i.isSelected());
	    acc.setLieferanten(j.isSelected());
	    acc.setMahnungen(k.isSelected());
	    acc.setOprechnungen(l.isSelected());
	    acc.setOrdonancen(m.isSelected());
	    acc.setPatienten(n.isSelected());
	    acc.setRechnungen(o.isSelected());
	    acc.setTarif(p.isSelected());
	    acc.setZytologiezettel(q.isSelected());
	    acc.setZytologiebuch(r.isSelected());
	    acc.setCashBook(cashBook.isSelected());

	    dataManager.getAccountContext().createAccount(a.getText(),
		    b.getText(), n.isSelected(), o.isSelected(),
		    l.isSelected(), k.isSelected(), r.isSelected(),
		    m.isSelected(), this.e.isSelected(), h.isSelected(),
		    g.isSelected(), i.isSelected(), d.isSelected(),
		    j.isSelected(), c.isSelected(), p.isSelected(),
		    f.isSelected(), q.isSelected());

	    dataManager.logIn(acc);
	    JOptionPane.showMessageDialog(dataManager,
		    "Das Konto wurde angelegt");
	    this.setVisible(false);
	}
	if (e.getActionCommand().equals("L" + UniCode.getoe() + "schen"))
	{
	    dataManager.getAccountContext().deleteAccount(a.getText());
	    JOptionPane.showMessageDialog(dataManager, "Das Konto wurde gel"
		    + UniCode.getoe() + "scht.");
	    this.setVisible(false);
	}
	if (e.getActionCommand().equals("Abbrechen"))
	{
	    this.setVisible(false);
	}
	if (e.getActionCommand()
		.equals("Passwort " + UniCode.getae() + "ndern"))
	{
	    dataManager.getAccountContext().changePassword(a.getText(),
		    b.getText());
	    JOptionPane.showMessageDialog(dataManager, "Das Passwort wurde ge"
		    + UniCode.getae() + "ndert.");
	    this.setVisible(false);
	}
    }
}
